package com.csd.controller;

import com.csd.dto.ActionResult;
import com.csd.dto.UserResult;
import com.csd.pojo.Admin;
import com.csd.pojo.SysUser;
import com.csd.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ Author :lihainan
 * @ ClassNmae :AdminController
 * @ Description :TODO
 * @ Date :2020/12/23
 * @ Version 1.0
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService ;

    /**
     * 管理员查看所有的用户
     * @return
     */
    @RequestMapping("/getSysUser")
    @ResponseBody
    public List<SysUser> getSysUser(){

        List<SysUser> sysUsers = adminService.findAllSysUser();
        return sysUsers;
    }

    /**
     * 通过id查询用户
      * @return
     */
    @RequestMapping(value = "/getUserInfoById/{id}",method = RequestMethod.GET)
    @ResponseBody
    private ActionResult getSysUserById(@PathVariable("id") int id){

        SysUser sysUsers = adminService.findAllSysUserById(id);
        return new ActionResult(200,null,sysUsers,1);
    }

    /**
     * 删除用户
     * @return
     */
    @RequestMapping(value = "/delUser",method = RequestMethod.DELETE)
    @ResponseBody
    public ActionResult getSysUserdel(int id){

        int code = adminService.delSysUserById(id);
        return new ActionResult(200,"del success",null,code);
    }
    /**
     * 修改用户
     * @return
     */
    @RequestMapping(value = "/modify",method = RequestMethod.PUT)
    public ActionResult modifyUser(@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        int code = adminService.modifySysUserById(sysUser);
        ActionResult result = new ActionResult(200, "modifysuccess", null, 1);
        System.out.println(result);
        return result;
    }
    /**
     * 管理员登录
     * @param admin
     * @return
     */
    @RequestMapping("/adminLogins")
    @ResponseBody
    public UserResult adminLogin(Admin admin){

        int code = 0;
        System.out.println(admin);
        Admin admin1 = adminService.findAdmin(admin);
        System.out.println(admin1);
        if(admin1 != null){
            code = 1;
        }
        UserResult userResult = new UserResult(200,"true",admin1 ,code);
        System.out.println(userResult);
        return userResult;
    }



}
